Ext.onReady(function() {
	Ext.Loader.setPath("org.jks", "view/common");
	var basePath = '\\';

	var searchPanel = Ext.create('Ext.toolbar.Toolbar', {
				dock : 'top',
				items : [{
					xtype : 'button',
					text : '新建文件夹',
					listeners : {
						click : function() {
							Ext.create("org.jks.CreateFolderWindow", {
								url : "attachment/createfolder",
								basePath : basePath,
								callback : function() {
									Ext.getCmp("resource_grid").getStore()
											.reload();
								}
							}).show();
						}
					}
				}, {
					xtype : 'button',
					text : '刷新',
					listeners : {
						click : function() {
							Ext.getCmp("resource_grid").getStore().reload();
						}
					}
				}, '-', {
					xtype : 'textfield',
					labelWidth : 60,
					id : 'name',
					fieldLabel : '资料名称'
				}, {
					xtype : 'button',
					text : '搜索',
					listeners : {
						click : function() {
							var store = Ext.getCmp("resource_grid").getStore();
							store.proxy.url = '/attachment/query';
							store.proxy.extraParams = {
								filename: Ext.getCmp('name').getValue()
							};
							store.reload();
						}
					}
				}, '-', {
					xtype : 'form',
					border : false,
					id : 'file_form',
					items : [{
								xtype : 'hiddenfield',
								id : 'hidden_base',
								name : 'base'
							}, {
								xtype : 'filefield',
								name : 'file',
								buttonMargin : 0,
								margin : 0,
								buttonConfig : {
									width : 70
								},
								buttonOnly : true,
								buttonText : '上传文件',
								listeners : {
									change : function(self, value, eOpts) {
										Ext.getCmp("file_form").submit({
											url : '/attachment/add',
											waitMsg : '上传文件中 ，请稍候……',
											success : function(form, action) {
												Ext.getCmp("resource_grid")
														.getStore()
														.reload();
											},
											failure: function(form, action){
												Ext.Msg.alert('错误',
															action.result.msg);
											
											}
										})
									}
								}
							}]
				}]

			});

	var flightResourceStore = Ext.create('Ext.data.Store', {
				fields : ['Id', 'Name', 'Path', {
							name : 'FileType',
							type : 'int'
						}, 'AddTime', {
							name : 'Type',
							type : 'object'
						}, {
							name : 'Flight',
							type : 'object'
						}],
				autoLoad : true,
				proxy : {
					type : 'ajax',
					url : '/attachment/list/',
					extraParams : {
						path : basePath
					},
					reader : {
						type : 'json',
						root : 'data.rows'
					}
				}
			});

	var refreshNavPath = function() {
		var store = Ext.getCmp("resource_grid").getStore();
		store.proxy.url = '/attachment/list';
		store.proxy.extraParams = {
			path : basePath
		};
		store.reload();
		Ext.getCmp('hidden_base').setValue(basePath);
		
		var paths = [];
		if(basePath != '\\'){
			paths = basePath.split('\\'); 
		}
		else{
			paths = [''];
		}
		var count = paths.length;
		var path = '\\';
		var records = [];
		for (var i = 0; i < count; i++) {
			if (i == 0) {
				path = '\\';
			} else if (i == 1) {
				path += paths[i];
			} else {
				path += '\\' + paths[i];
			}
			var label = '';
			if (i == 0) {
				label = '根路径';
			} else {
				label = paths[i];
			}
			Ext.Array.include(records, {
						url : path,
						label : label
					});
		}
		pathStore.loadData(records);
	}

	var flightResource = Ext.create('Ext.grid.Panel', {
		id : 'resource_grid',
		margin : '0 5 5 5',
		store : flightResourceStore,
		listeners : {
			cellclick : function(self, td, cellIndex, record, tr, rowIndex, e,
					eOpts) {
				if (record.get("FileType") == 1 && (cellIndex==1 || cellIndex==5)) {
					if (Ext.String.endsWith(basePath, '\\')) {
						basePath += record.get('Name');
					} else {
						basePath += '\\' + record.get('Name');
					}
					refreshNavPath();
				}
			}
		},
		columns : [{
					text : '序号',
					xtype : 'rownumberer',
					width : 40,
					sortable : false
				}, {
					text : '名称',
					dataIndex : 'Name',
					flex : 1,
					renderer : function(v, meta, record) {
						if (record.get('FileType') == 0) {
							return '[-]&nbsp;&nbsp;' + v;
						}
						return '[d]&nbsp;&nbsp;<a>' + v + '</a>';
					}
				},{
					text: '路径',
					dataIndex: 'Path',
					flex:1
				}, {
					text : '上传时间',
					dataIndex : 'AddTime',
					width : 200
				}, {
					text : '班组',
					dataIndex : 'Flight',
					width : 200,
					renderer : function(v) {
						if (v) {
							return v.Name;
						}
						return 'admin';
					}
				}, {
					text : '',
					width : 60,
					renderer : function(v, meta, record) {
						return record.get('FileType') == 0
								? '<a href="attachment/download/'
										+ record.get('Name') + '?filepath='+record.get('Path')+'">下载</a>'
								: '<a>进入</a>';
					}
				}, {
					text : '',
					xtype : 'actioncolumn',
					width : 50,
					items : [{
						icon : 'view/images/delete.png',
						tooltip : 'Delete',
						handler : function(grid, rowIndex, colIndex) {
							var record = grid.getStore().getAt(rowIndex);
							Ext.getCmp("resource_grid").setLoading(true);
							var url = 'attachment/delete/' + record.get('Id');
							Ext.Ajax.request({
								url : url,
								callback : function(options, success, response) {
									Ext.getCmp("resource_grid")
											.setLoading(false);
									if (success) {
										var ar = Ext
												.decode(response.responseText);
										if (!ar.success) {
											Ext.Msg.alert('错误', ar.msg);
										} else {
											var store = Ext.getCmp("resource_grid")
													.getStore();
											store.reload();
										}
									}
								}
							})
						}
					}]
				}]
	});

	Ext.define('PathUrl', {
				extend : 'Ext.data.Model',
				fields : [{
							name : 'url',
							type : 'string'
						}, {
							name : 'label',
							type : 'string'
						}]
			});

	var pathStore = Ext.create('Ext.data.Store', {
				model : 'PathUrl',
				data : [{
							url : '\\',
							label : '根路径'
						}]
			});
	var pathTpl = new Ext.XTemplate(
			'<tpl for=".">',
			'<tpl if="this.check(xindex,xcount)">',
			'<span style="font-size:14px;" class="pathitem">{label}</span>',
			'</tpl>',
			'<tpl if="!this.check(xindex,xcount)">',
			'<span style="font-size:14px;color:blue;cursor:hand;" class="pathitem">{label}</span><b>\\\\</b>',
			'</tpl>', '</tpl>', {
				check : function(xindex, xcount) {
					if (xindex == xcount) {
						return true;
					} else {
						return false;
					}
				}
			});

	var pathView = Ext.create('Ext.view.View', {
				id : 'pathview',
				store : pathStore,
				tpl : pathTpl,
				itemSelector : 'span.pathitem',
				emptyText : '',
				listeners : {
					itemclick : function(self, record, item, index, e, eOpts) {
						basePath = record.get('url');
						var store = self.getStore();
						var count = store.getCount();
						if(store.getAt(count-1)!=record){
							refreshNavPath();
						}
						
					}
				}
			});

	var panel = Ext.create("Ext.panel.Panel", {
				border : false,
				bodyBorder : false,
				layout : 'fit',
				dockedItems : [searchPanel, {
							xtype : 'container',
							id : 'current_path',
							margin : '5 5',
							dock : 'top',
							layout : 'hbox',
							items : [{
										xtype : 'component',
										margin : '0 10 0 0',
										style : "font-size:14px;",
										html : '路径:'
									}, pathView]
						}],
				items : [flightResource]
			});
	Ext.create('Ext.Viewport', {
				layout : 'fit',
				style : 'background:none;background-color:white;',
				items : [panel]
			});
});